﻿using System;
using System.Web.Mvc;
using Newtonsoft.Json;
using WebSite.Class;
using WebSite.Models;
using Xwltz.Framework.Helper;

namespace WebSite.Controllers
{
    public class HomeController : BaseController
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Code()
        {
            var helper = new CaptchaHelper();
            var code = helper.CreateValidateCode(4);
            var value = helper.CreateImage2(code);

            TempData["code"] = code; //存储验证码用于验证

            //将验证码绘制到图片上、保存到内存流中并返回字节数组
            return File(value, "image/jpeg");
        }

        [HttpPost]
        public ActionResult Login(string mobile, string password, string code)
        {
            if (code.IsEmpty())
            {
                ViewData["msg"] = "请输入验证码！";
                SaveAccountLog(LogTypeEnum.SignIn, $"[{mobile}]" + ViewData["msg"]);
                return View();
            }

            var vCode = TempData["code"].ToString();
            if (vCode.IsEmpty())
            {
                ViewData["msg"] = "请重新获取验证码并登录！";
                SaveAccountLog(LogTypeEnum.SignIn, $"[{mobile}]" + ViewData["msg"]);
                return View();
            }

            if (!code.ToUpper().Equals(vCode.ToUpper()))
            {
                ViewData["msg"] = "验证码输入错误，请重新获取验证码并登录！";
                SaveAccountLog(LogTypeEnum.SignIn, $"[{mobile}]" + ViewData["msg"]);
                return View();
            }

            var accountModel = Db.Queryable<Account>().Single(x => x.Mobile.Equals(mobile));
            if (accountModel == null)
            {
                var result = GetByMobile(mobile);
                if (result.IsNotEmpty())
                {
                    return Content(result);
                }
            }

            accountModel = Db.Queryable<Account>().Single(x => x.Mobile.Equals(mobile));
            var check = CryptoService.AESEncrypt(password, accountModel.Salt);
            if (!check.Equals(accountModel.Password))
            {
                ViewData["msg"] = "账号或密码错误，请稍后重试或联系系统管理员！";
                SaveAccountLog(LogTypeEnum.SignIn, $"[{mobile}]" + ViewData["msg"]);
                return View();
            }

            // 登录成功 并且 跳转至首页
            Session["AccountModel"] = JsonConvert.SerializeObject(accountModel);
            accountModel.LastLoginTime = DateTime.Now;
            Db.Updateable(accountModel).ExecuteCommand();
            SaveAccountLog(LogTypeEnum.SignIn, $"[{mobile}]登录成功！");
            accountModel.Password = "";
            return RedirectToAction("Index", "Home");
        }

        [HttpGet]
        public ActionResult SignOut()
        {
            SaveAccountLog(LogTypeEnum.SignOut);
            Session["AccountModel"] = null;
            return RedirectToAction("Login", "Home");
        }
    }
}